﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Views.Grid;
using SmallERP.MODEL.FenPian;
using SmallERP.MODEL.FoundationModel;
using SmallERP.MODEL.PBModel;
using SmallERP.PinBan.ImportPhoto.Forms;
using SmallERP.PinBan.ImportPhoto.Presenter;
using SmallERP.PinBan.ImportPhoto.View;
using Smallerp.Contracts.DataContracts;
using Smallerp.Contracts;
using System.ServiceModel;
using Leadtools.Codecs;
using System.Runtime.InteropServices;

using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Base.ViewInfo;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using Lucene.Net.Analysis;
using SmallERP.PinBan.PB.Presenter;
using SmallERP.PinBan;
using SmallERP.COM;


namespace Small.PinBan
{
    public partial class FrmImportPhoto : Form, IFrmImportPhoto
    {
        public FrmImportPhoto()
        {
            InitializeComponent();

        }

        private static FrmImportPhoto _instance;
        public static FrmImportPhoto GetInstance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new FrmImportPhoto();
                }
                return _instance;

            }
        }


        private ImportPhotoPresenter _presenter;
        private void FrmImportPhoto_Load(object sender, EventArgs e)
        {
            IniFile.FilePath = Application.StartupPath;
            _presenter = new ImportPhotoPresenter(this);
            InitForm();
            List<string> list = new List<string>();
            RasterCodecs.Startup();
            buttonEditFile.Text = IniFile.DxfFolderPath;
            // GetAllPicByDir(@"D:\Projects", list);
            FileNamelist = GetDxfFile(IniFile.DxfFolderPath);
            BindDXFList();
        }

        public void InitForm()
        {

            //repositoryItemLookUpEditPaperType.Columns.Add(new LookUpColumnInfo ("PaperTypeId", 40, "编号"));
            //repositoryItemLookUpEditPaperType.Columns.Add(new LookUpColumnInfo("PaperTypeName", 40, "名称"));
            //repositoryItemLookUpEditPaperType.DisplayMember = "PaperTypeName";
            //repositoryItemLookUpEditPaperType.ValueMember = "PaperTypeId";
            //repositoryItemLookUpEditPaperType.DataSource = TypeBind;
            comboBoxPriority.SelectedIndex = 0;
            this.gridControlFileList.DataSource = this.OrderViewList;
            gridControlFileList.RefreshDataSource();
            //tabControlWork.Visible = false;
            //tabPageImportList.Visible = false;
            tabPageAutoImport.Visible = false;
            // tabPageImportList.PageVisible = false;
            tabPageAutoImport.PageVisible = false;
            foreach (PaperTypeDCT info in TypeBind)
            {
                repositoryItemComboBoxPaperTyye.Items.Add(info.PaperTypeName);
                repositoryItemComboBoxphotoPaperTyye.Items.Add(info.PaperTypeName);
            }
            foreach (PaperFaceDCT info in FaceBind)
            {
                repositoryItemComboBoxPaperFace.Items.Add(info.PaperFaceName);
                repositoryItemComboBoxphotoPaperFace.Items.Add(info.PaperFaceName);
            }
            foreach (PrinterDCT info in PrintBind)
            {
                repositoryItemComboBoxPrinter.Items.Add(info.PName);
            }

            RasterCodecs.Startup();
            string MinLengthPrs = IniFile.IniReadValue("PB", "CurrSelectPath");

            string currselect = MinLengthPrs;
            string path = SelectDir(currselect);
            if (string.IsNullOrEmpty(path))
            {
                browser1.StartUpDirectory = FileBrowser.SpecialFolders.Desktop;
                browser1.SelectPath(FileBrowser.SpecialFolders.Desktop, true);
            }
            else
            {
                //browser1.StartUpDirectoryOther = path;
                browser1.SelectPath(path, true);
                //browser1.
                //browser1.Select();
            }
            browser1.Refresh();
            //if (Directory.Exists(currselect))
            //{
            //    browser1.StartUpDirectoryOther = currselect;

            //}
            //else
            //{
            //    currselect = currselect.Substring(0, currselect.LastIndexOf("\\"));
            //}
            IniFile.FilePath = Application.StartupPath;
            string strCopyRule = IniFile.IniReadValue("PB", "CopyRule");

            if (string.IsNullOrEmpty(strCopyRule))
            {
                IniFile.IniWriteValue("PB", "CopyRule", "");

            }
            else
            {
                ImportDataTransfer.CopyRule = strCopyRule;
                string[] ruleArray = strCopyRule.Split(',');
                foreach (string str in ruleArray)
                {
                    listBoxCopyRule.Items.Add(str);
                }

            }
            string strUseCopyRule = IniFile.IniReadValue("PB", "UseCopyRule");
            if (string.IsNullOrEmpty(strUseCopyRule))
            {
                listBoxCopyRule.Enabled = false;
                textBoxRule.Enabled = false;
                buttonAddRule.Enabled = false;
                buttonDelRule.Enabled = false;
                ImportDataTransfer.UseCopyRule = false;
            }
            else
            {

                if ("true".Equals(strUseCopyRule))
                {
                    checkBoxUseCopyRule.Checked = true;
                    listBoxCopyRule.Enabled = true;
                    textBoxRule.Enabled = true;
                    buttonAddRule.Enabled = true;
                    buttonDelRule.Enabled = true;
                    ImportDataTransfer.UseCopyRule = true;


                }
                else
                {
                    listBoxCopyRule.Enabled = false;
                    textBoxRule.Enabled = false;
                    buttonAddRule.Enabled = false;
                    buttonDelRule.Enabled = false;
                    ImportDataTransfer.UseCopyRule = false;
                }
            }


        }
        string SelectDir(string inPath)
        {
            if (string.IsNullOrEmpty(inPath))
            {
                return "";
            }
            if (inPath.IndexOf("\\") > 0)
            {
                if (Directory.Exists(inPath))
                {
                    return inPath;
                }
                else
                {

                    return SelectDir(inPath.Substring(0, inPath.LastIndexOf("\\")));
                }
            }
            else
            {
                return "";
            }


        }


        private void repositoryItemLookUpEditPaperType_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void gridViewFile_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            //if(e.Column.Name=="paperType")
            //{
            //    OrderDDO ddo = ((OrderDDO) gridViewFile.GetRow(e.RowHandle));

            //}
        }


        public PaperFaceDCT PaperFaceInfo
        {
            get;
            set;
        }

        public PaperTypeDCT PaperTypeInfo
        {
            get;
            set;
        }

        public PrinterDCT PrinterInfo
        {
            get;
            set;
        }

        public int PrintType
        {
            get;
            set;
        }

        public int IntPriority
        {
            get;
            set;
        }

        public string OrderPath
        {
            get;
            set;
        }

        public OrderViewDCT _currOrder
        {
            get;
            set;
        }

        public List<OrderViewDCT> OrderViewList
        {
            get;
            set;
        }

        public OrderDDO OrderViewDCT
        {
            get;
            set;
        }

        public List<PaperFaceDCT> FaceBind
        {
            get;
            set;
        }

        public List<PaperTypeDCT> TypeBind
        {
            get;
            set;
        }

        public List<PrinterDCT> PrintBind
        {
            get;
            set;
        }

        public bool CanClose
        {
            get;
            set;
        }

        public string CurrImportPath
        {
            get;
            set;
        }

        public List<string> CurrTaskPath
        {
            get;
            set;

        }

        public List<OrderViewDCT> OrderErrorList
        {
            get;
            set;
        }

        public bool IsAnalyIng
        {
            get;
            set;
        }
        public ChannelFactory<IOperateDB> _proxyOperateDB { get; set; }//创建代理
        public IOperateDB _OperateDb { get; set; }
        public void BingPaperFace()
        {
            comboBoxFace.DisplayMember = "PaperFaceName";
            comboBoxFace.ValueMember = "PaperFaceId";
            comboBoxFace.DataSource = FaceBind;
            if (FaceBind.Count > 0)
            {
                //comboBoxFace.SelectedIndex = 0;
                comboBoxFace.SelectedValue = DataTransfer.curTemplateInfo.PaperFaceId;
                PaperFaceInfo = ((PaperFaceDCT)comboBoxFace.SelectedItem);
            }
        }

        public void BingPaperType()
        {
            comboBoxType.DisplayMember = "PaperTypeName";
            comboBoxType.ValueMember = "PaperTypeId";
            comboBoxType.DataSource = TypeBind;
            if (TypeBind.Count > 0)
            {
                //comboBoxType.SelectedIndex = 0;
                comboBoxType.SelectedValue = DataTransfer.curTemplateInfo.PaperTypeId;
                PaperTypeInfo = ((PaperTypeDCT)comboBoxType.SelectedItem);
            }
        }

        public void BingPrint()
        {
            comboBoxPrint.DisplayMember = "PName";
            comboBoxPrint.ValueMember = "PrinterID";
            comboBoxPrint.DataSource = PrintBind;
            if (PrintBind.Count > 0)
            {
                comboBoxPrint.SelectedIndex = 0;
                PrinterInfo = ((PrinterDCT)comboBoxPrint.SelectedItem);
            }
        }

        private void comboBoxType_DropDownClosed(object sender, EventArgs e)
        {
            PaperTypeInfo = ((PaperTypeDCT)comboBoxType.SelectedItem);
        }

        private void comboBoxFace_DropDownClosed(object sender, EventArgs e)
        {
            PaperFaceInfo = ((PaperFaceDCT)comboBoxFace.SelectedItem);
        }

        private void comboBoxPrint_DropDownClosed(object sender, EventArgs e)
        {
            PrinterInfo = ((PrinterDCT)comboBoxPrint.SelectedItem);
        }

        private void gridControlFileList_DragDrop(object sender, DragEventArgs e)
        {

            // string tagartDir = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
            List<string> listStrDir = new List<string>();
            // Array listDir = ((System.Array) e.Data.GetData(DataFormats.FileDrop));
            // if(e.Data.GetDataPresent(DataFormats.Text))

            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                object data = e.Data.GetData(DataFormats.FileDrop);

                string[] paths = (string[])data;

                foreach (string array in paths)
                {
                    if (Directory.Exists(array))
                    {
                        if (OrderViewList.FirstOrDefault(p => p.OrilOrderPath.Equals(array)) != null)
                        {
                            continue;
                        }
                        //if (File.Exists(array + "\\Properties.ini"))
                        //{
                        //    if (MessageBox.Show("文件包 " + array + "重复导入，是否确定导入", "询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                        //    {
                        //        listStrDir.Add(array.ToString());
                        //    }
                        //}
                        //else
                        //{
                        //    listStrDir.Add(array.ToString());
                        //}
                        if (ImportDataTransfer.FileSavePath == "0")
                        {
                            if (File.Exists(array + "\\Properties.ini"))
                            {
                                if (
                                    MessageBox.Show("文件包 " + array + "重复导入，是否确定导入", "询问", MessageBoxButtons.OKCancel,
                                        MessageBoxIcon.Question) == DialogResult.OK)
                                {
                                    listStrDir.Add(array.ToString());
                                }
                            }
                            else
                            {
                                listStrDir.Add(array.ToString());
                            }
                        }
                        else
                        {
                            string orderName = array.Substring(array.LastIndexOf("\\") + 1);
                            bool isReplece = _presenter.CheckOrder(orderName);
                            if (isReplece)
                            {
                                if (
                                    MessageBox.Show("文件包 " + array + "重复导入，是否确定导入", "询问", MessageBoxButtons.OKCancel,
                                        MessageBoxIcon.Question) == DialogResult.OK)
                                {
                                    listStrDir.Add(array.ToString());
                                }
                            }
                            else
                            {
                                listStrDir.Add(array.ToString());
                            }

                        }
                    }
                }
                this.Cursor = Cursors.WaitCursor;
                AnlyseFilePath = listStrDir;
                _presenter.CreateOrderView(listStrDir);

                this.Cursor = Cursors.Default;
                //MessageBox.Show("", "");
            }
            else if (e.Data.GetDataPresent(DataFormats.Text))
            {
                string dxfFilePath= e.Data.GetData(DataFormats.Text) as string;
                // var row=
                GridControl grid = sender as GridControl; 
                GridView view = gridViewPhotoList as GridView;
                Point point = new Point(e.X, e.Y);
                point= grid.PointToClient(point);
                DevExpress.XtraGrid.Views.Base.BaseView baseView = grid.GetViewAt(point);
                if (baseView == null)                                                              
                {
                    baseView = grid.FocusedView;
                }
                //BaseHitInfo calcHitInfo = baseView.CalcHitInfo(grid.PointToClient(point));

                GridHitInfo  hitInfo = baseView.CalcHitInfo(grid.PointToClient(new Point(e.X, e.Y))) as GridHitInfo;
                int rowData = hitInfo.RowHandle;
                PhotoDCT photoDct = baseView.GetRow(rowData) as PhotoDCT;
                photoDct.DXFFilePath = dxfFilePath;
                photoDct.DXFContent = DxfExtend.GetFileContent(dxfFilePath);
                view.RefreshRow(rowData);
                //int targetRow = hitInfo.RowHandle;

            }

        }
        List<string> AnlyseFilePath = new List<string>();


        private void gridControlFileList_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effect = DragDropEffects.Move;
            else if (e.Data.GetDataPresent(DataFormats.Text))
                e.Effect = DragDropEffects.Copy;
            else e.Effect = DragDropEffects.None;
        }

        private void comboBoxPriority_DropDownClosed(object sender, EventArgs e)
        {
            IntPriority = comboBoxPriority.SelectedIndex;
        }

        private void gridViewFile_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
        }
        void BingDinViewCreate()
        {
            this.gridControlFileList.DataSource = this.OrderViewList;
            gridControlFileList.RefreshDataSource();
        }

        public void RefreshDataCreateView()
        {

            gridControlFileList.RefreshDataSource();
        }
        delegate void UpRechText(string msg, Color co);
        //public void ShowImportMessage(string inStr)
        //{
        //    if (richTextBoxInfo.InvokeRequired)
        //    {
        //        this.Invoke(new UpRechText(ShowImportMessage), new object[] { inStr });

        //        return;
        //    }
        //    else
        //    {
        //        splitContainerBottom.Panel2Collapsed = false;
        //        richTextBoxInfo.Text = inStr + richTextBoxInfo.Text;

        //    }
        //}
        public void ShowImportMessage(string inStr, Color inColor)
        {
            if (richTextBoxInfo.InvokeRequired)
            {
                this.Invoke(new UpRechText(ShowImportMessage), new object[] { inStr, inColor });

                return;
            }
            else
            {
                splitContainerBottom.Panel2Collapsed = false;
                richTextBoxInfo.Text = inStr + richTextBoxInfo.Text;
                richTextBoxInfo.SelectionStart = 0;
                if (inStr.Length < 1)
                {
                    inStr = "a";
                }
                richTextBoxInfo.SelectionLength = inStr.Length - 1;
                richTextBoxInfo.SelectionColor = inColor;
                richTextBoxInfo.SelectionLength = 0;

            }
        }



        private delegate void ShowView(List<OrderViewDCT> viewList);
        public void RushViewData(List<OrderViewDCT> viewList)
        {
            if (gridControlFileList.InvokeRequired)
            {
                this.Invoke(new ShowView(RushViewData), new object[] { viewList });
            }
            else
            {
                if (viewList.Count == 0)
                {
                    //this.buttonOk.Enabled = true;
                    ThawyFrm();
                    this.Cursor = Cursors.Default; ;
                }
                else
                {
                    //    this.buttonOk.Enabled = false;
                    FreezeFrm();
                    this.Cursor = Cursors.WaitCursor;
                }
                gridControlFileList.DataSource = viewList;
                gridControlFileList.RefreshDataSource();
            }
        }

        private delegate void ShowFileName(string instr);
        public void ShowReadFileName(string strFileName)
        {
            if (labelReadFile.InvokeRequired)
            {
                this.Invoke(new ShowFileName(ShowReadFileName), new object[] { strFileName });
            }
            else
            {
                this.labelReadFile.Text = strFileName;

            }
        }
        private delegate void SetProcess(int inPoint, int inLength);
        public void SetProcessValue(int inPoint, int inLength)
        {
            if (progressBarReadFile.InvokeRequired)
            {
                this.Invoke(new SetProcess(SetProcessValue), new object[] { inPoint, inLength });
            }
            else
            {


                progressBarReadFile.Maximum = inLength;
                progressBarReadFile.Value = inPoint;
            }

        }

        public void FreezeFrm()
        {
            this.gridControlFileList.AllowDrop = false;
            this.panelOper1.Enabled = false;
            //   panelOper.Enabled = false;
            gridControlFileList.Enabled = false;
        }
        private delegate void DeleThawyFrm();
        private delegate void DelePanel();
        private delegate void DeleGridControlFileList();
        public void ThawyFrm()
        {
            if (gridControlFileList.InvokeRequired)
            {
                this.Invoke(new DeleThawyFrm(ThawyFrm), new object[] { });
            }
            else
            {
                this.gridControlFileList.AllowDrop = true;
            }
            if (panelOper1.InvokeRequired)
            {
                this.Invoke(new DelePanel(ThawyFrm), new object[] { });
            }
            else
            {
                this.panelOper1.Enabled = true;
            }

            //panelOper.Enabled = true;
            if (gridControlFileList.InvokeRequired)
            {
                this.Invoke(new DeleGridControlFileList(ThawyFrm), new object[] { });
            }
            else
            {

                gridControlFileList.DataSource = OrderViewList;
                //gridControlFileList.DataSource = null;
                gridControlFileList.RefreshDataSource();
                gridControlFileList.Enabled = true;


            }
        }

        public void DisPlayErrorList()
        {
            gridControlErrList.DataSource = this.OrderErrorList;
            gridControlErrList.RefreshDataSource();
        }
        private delegate void DelegtBrease();
        private delegate void DelegProssAnly();
        public void SetOperatSatus()
        {

            if (gridControlFileList.InvokeRequired)
            {
                this.Invoke(new DelegtBrease(SetOperatSatus), new object[] { });
            }
            else
            {
                if (this.IsAnalyIng)
                {
                    this.Cursor = Cursors.WaitCursor;
                    gridControlFileList.AllowDrop = false;
                    timerAnlyse.Start();


                }
                else
                {
                    this.Cursor = Cursors.Default;
                    gridControlFileList.AllowDrop = true;
                    timerAnlyse.Stop();
                    progressBarReadFile.Value = 0;
                }
            }
            //if (timerAnlyse.i)
            //{
            //    this.Invoke(new DelegtBrease(SetOperatSatus), new object[] { });
            //}
            //else
            //{
            //    if (this.IsAnalyIng)
            //    {
            //        timerAnlyse.Start();

            //    }
            //    else
            //    {
            //        timerAnlyse.Stop();
            //    }
            //}

        }
        //Timer timeAnalse = new Timer();

        public void CloseAnaly()
        {
            //progressBarReadFile.Minimum = 0;
            //progressBarReadFile.Maximum = 100;
            //this.Cursor = Cursors.Default;
        }


        private void buttonModify_Click(object sender, EventArgs e)
        {
            //gridViewFile.getg
            if (_currOrder != null)
            {
                FrmModifyPara frmModifyPara = new FrmModifyPara(_currOrder, _OperateDb);
                frmModifyPara.ShowDialog();
                gridControlFileList.RefreshDataSource();
            }
            else
            {
                MessageBox.Show("请选择要修改的产品", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }
        //OrderViewDCT _
        int modefyType = 0;
        int clickType = 0;
        private void gridViewFile_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            clickType = 0;
            if (e.RowHandle >= 0 && e.Button == MouseButtons.Left)
            {


                listBoxMuliModefy.Items.Clear();
                listBoxMuliModefy.Visible = false;
                _currOrder = (OrderViewDCT)gridViewFile.GetRow(e.RowHandle);
            }
            else if (e.RowHandle >= 0 && e.Button == MouseButtons.Right)
            {
                if (e.Column.Name == "paperType")
                {
                    listBoxMuliModefy.Items.Clear();
                    listBoxMuliModefy.Visible = true;
                    listBoxMuliModefy.Location = e.Location;
                    foreach (PaperTypeDCT info in TypeBind)
                    {
                        listBoxMuliModefy.Items.Add(info.PaperTypeName);
                    }
                    modefyType = 1;//纸型
                }
                else if (e.Column.Name == "PaperFace")
                {
                    listBoxMuliModefy.Items.Clear();
                    listBoxMuliModefy.Visible = true;
                    listBoxMuliModefy.Location = e.Location;
                    foreach (PaperFaceDCT info in FaceBind)
                    {
                        listBoxMuliModefy.Items.Add(info.PaperFaceName);
                    }
                    modefyType = 2;//纸面
                }
                else
                {
                    listBoxMuliModefy.Items.Clear();
                    listBoxMuliModefy.Visible = false;
                }
            }
            else
            {

                listBoxMuliModefy.Items.Clear();
                listBoxMuliModefy.Visible = false;
            }
        }

        private void buttonDel_Click(object sender, EventArgs e)
        {
            int[] listRowID = gridViewFile.GetSelectedRows();
            if (listRowID.Length > 0)
            {
                if (DialogResult.OK == MessageBox.Show("确认删除选择的记录", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
                {
                    //OrderViewList.Remove(_currOrder);
                    //gridControlFileList.RefreshDataSource();
                    List<OrderViewDCT> ExViewList = new List<OrderViewDCT>();
                    OrderViewDCT exView;

                    foreach (int i in listRowID)
                    {
                        exView = (OrderViewDCT)gridViewFile.GetRow(i);
                        ExViewList.Add(exView);
                    }
                    OrderViewList = OrderViewList.Except(ExViewList).ToList();
                    gridControlFileList.DataSource = OrderViewList;
                    gridControlFileList.RefreshDataSource();

                }
            }
            else
            {
                MessageBox.Show("未选择任何记录", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        public delegate void RushControl();

        public event RushControl Rush;
        private void buttonOk_Click(object sender, EventArgs e)
        {
            IniFile.FilePath = Application.StartupPath;
            IniFile.IniWriteValue("PB", "CurrSelectPath", browser1.SelectedItem.Path);

            PBDataTransfer.CurrSelectPath = browser1.SelectedItem.Path;

            //this.buttonOk.Enabled = false;
            // this.gridControlFileList.AllowDrop = false;
            this.panelOper1.Enabled = false;
            //this.panelOper.Enabled = false;
            this.gridControlFileList.Enabled = false;
            gridControlFileList.AllowDrop = false;
            this.Cursor = Cursors.WaitCursor;
            if (((List<OrderViewDCT>)gridControlFileList.DataSource).Count > 0)
            {
                _presenter.AddToDB();
                //_presenter.MoveDir();
            }
            gridControlFileList.Enabled = true;

            //this.panelOper.Enabled = true;
            this.Cursor = Cursors.Default;
            if (((List<OrderViewDCT>)gridControlFileList.DataSource).Count == 0)
            {
                Rush();
                // this.Close();
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            _presenter.MoveDir();
        }

        private void FrmImportPhoto_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.CanClose)
            {
                if (DialogResult.OK == MessageBox.Show("有文件正在移动确认要关闭吗？未移动完成的照片将在下次启动时继续移动", "确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
                {
                    e.Cancel = false;
                }
                else
                {
                    e.Cancel = true;
                }
            }
            else
            {
                if (this.IsAnalyIng)
                {
                    if (DialogResult.OK == MessageBox.Show("正在分析文件确认要关闭吗？", "确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
                    {
                        e.Cancel = false;
                    }
                    else
                    {
                        e.Cancel = true;
                    }
                }



            }
            _instance = null;
            RasterCodecs.Shutdown();
        }

        private void buttonComplex_Click(object sender, EventArgs e)
        {
            //browser1.SelectedItem.
            CurrImportPath = browser1.SelectedItem.Path;
            //browser1.fol
            //CurrImportPath = browser1.SelectedFileName;
            string flag = CurrImportPath.Substring(1, 1);
            if (!":".EndsWith(flag))
            {
                MessageBox.Show("不支持桌面、我的文档等系统文件夹导入，请拷贝到物理盘符下导入", "确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                return;
            }
            if ((!string.IsNullOrEmpty(CurrImportPath)) && MessageBox.Show("确定把文件夹 " + CurrImportPath + " 下的所有文件夹导入？", "确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.OK)
            {
                _presenter.CreateComplexView();
            }
            CurrImportPath = "";
            IniFile.IniWriteValue("PB", "CurrSelectPath", browser1.SelectedItem.Path);

            PBDataTransfer.CurrSelectPath = browser1.SelectedItem.Path;
        }

        private void browser1_SelectedFolderChanged(object sender, FileBrowser.SelectedFolderChangedEventArgs e)
        {
            // this.CurrImportPath = e.Path;

        }

        private void gridViewFile_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                OrderViewDCT view = (OrderViewDCT)gridViewFile.GetRow(e.RowHandle);
                if (view != null)
                {
                    if (e.Column.Name == "MoveStatus")
                    {
                        if (!string.IsNullOrEmpty(view.MoveStatus))
                        {

                            e.DisplayText = "正在移动";
                        }

                        else
                        {
                            if (view.OrderProductId != 0)
                            {
                                e.DisplayText = "等待移动";
                            }
                            else
                            {
                                e.DisplayText = "未导片";
                            }
                        }
                    }
                    if (e.Column.Name == "Priority")
                    {
                        if (view.Priority == 0)
                        {
                            e.DisplayText = "普通";
                        }
                        else
                        {
                            e.DisplayText = "加急";
                        }
                    }
                }
            }
        }
        #region//子view相关


        public bool CompareView(PhotoDCT view1, PhotoDCT view2)
        {
            if (view1 == null || view2 == null || view1.Height == null || view2.Height == null)
            {
                return false;
            }
            if ((view1.Height == view2.Height && view1.Width == view2.Width) || (view1.Width == view2.Height) && view1.Height == view2.Width)
            {
                return true;
            }
            else
            {
                return false;

            }
        }

        private string strCurrStandard = "";
        //   private PhotoDCT viewCurrStanderd=new PhotoDCT();
        private void gridViewFile_MasterRowGetChildList(object sender, DevExpress.XtraGrid.Views.Grid.MasterRowGetChildListEventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            if (e.RowHandle >= 0)
            {
                OrderViewDCT view = (OrderViewDCT)gridViewFile.GetRow(e.RowHandle);
                e.ChildList = view.PhotoS;
                //List < PhotoDCT > views= view.PhotoS;
                //  var ss = view.PhotoS.GroupBy(p => p.SizeInch,new MyPhotoSizeComparer()).ToList();
                var ss = view.PhotoS.GroupBy(p => p.SizeInch).ToList();


                int maxSize = 0;

                foreach (IGrouping<string, PhotoDCT> list in ss)
                {
                    //if (CompareView((PhotoDCT)list,viewCurrStanderd))
                    //{
                    //    maxSize += list.Count();

                    //}
                    if (list.Count() > maxSize)
                    {
                        maxSize = list.Count();
                        strCurrStandard = list.Key;
                        // viewCurrStanderd = (PhotoDCT)list;
                    }
                    else
                    {

                    }
                }
                //foreach (PhotoDCT list in views)
                //{
                //    if (list.Height>list.Width)
                //    {


                //    }
                //}


            }
            this.Cursor = Cursors.Default;
        }

        private void gridViewFile_MasterRowEmpty(object sender, DevExpress.XtraGrid.Views.Grid.MasterRowEmptyEventArgs e)
        {
            e.IsEmpty = false;
        }

        private void gridViewFile_MasterRowGetRelationName(object sender, DevExpress.XtraGrid.Views.Grid.MasterRowGetRelationNameEventArgs e)
        {
            e.RelationName = "照片";
        }

        private void gridViewFile_MasterRowGetRelationCount(object sender, DevExpress.XtraGrid.Views.Grid.MasterRowGetRelationCountEventArgs e)
        {
            e.RelationCount = 1;
        }

        private void gridViewFile_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (e.FocusedRowHandle >= 0)
            {
                _currOrder = (OrderViewDCT)gridViewFile.GetRow(e.FocusedRowHandle);
            }
        }
        #endregion

        /// <summary>
        /// 批量修改 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listBoxMuliModefy_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selectValue = listBoxMuliModefy.SelectedItem.ToString();
            listBoxMuliModefy.Visible = false;
            //一下修改数据
            int[] arrRowID = gridViewFile.GetSelectedRows();
            if (arrRowID.Count() > 1)
            {
                string strType = "";
                if (modefyType == 1)
                {
                    strType = "纸型";
                }
                else
                {
                    strType = "纸面";
                }

                if (MessageBox.Show("确定要修改选中记录的”" + strType + "“为”" + selectValue + "“", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.OK)
                {
                    foreach (int var in arrRowID)
                    {
                        OrderViewDCT view = (OrderViewDCT)gridViewFile.GetRow(var);
                        if (modefyType == 1)//纸型
                        {
                            view.PaperTypeName = selectValue;
                        }
                        else if (modefyType == 2)
                        {
                            view.PaperFaceName = selectValue;
                        }

                    }
                }
            }
            else
            {
                MessageBox.Show("您只有选中一条记录，请直接点击修改", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            gridControlFileList.RefreshDataSource();
            listBoxMuliModefy.Visible = false;

        }

        private void tabControlWork_SelectedPageChanged(object sender, DevExpress.XtraTab.TabPageChangedEventArgs e)
        {
            if (tabControlWork.SelectedTabPageIndex == 1)
            {
                _presenter.GetErrList();

            }
        }

        private void gridViewErrList_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
        }

        private void gridViewPhotoErrList_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
        }

        private void gridViewErrList_MasterRowEmpty(object sender, DevExpress.XtraGrid.Views.Grid.MasterRowEmptyEventArgs e)
        {
            e.IsEmpty = false;
        }

        private void gridViewErrList_MasterRowGetRelationName(object sender, DevExpress.XtraGrid.Views.Grid.MasterRowGetRelationNameEventArgs e)
        {
            e.RelationName = "照片";
        }

        private void gridViewErrList_MasterRowGetRelationCount(object sender, DevExpress.XtraGrid.Views.Grid.MasterRowGetRelationCountEventArgs e)
        {
            e.RelationCount = 1;
        }

        private void gridViewErrList_MasterRowGetChildList(object sender, DevExpress.XtraGrid.Views.Grid.MasterRowGetChildListEventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            if (e.RowHandle >= 0)
            {
                OrderViewDCT view = (OrderViewDCT)gridViewErrList.GetRow(e.RowHandle);
                e.ChildList = view.PhotoS;

            }
            this.Cursor = Cursors.Default;
        }

        private void buttonDelErr_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == MessageBox.Show("确认删除选中的数据", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information))
            {
                int[] arrRowId = gridViewErrList.GetSelectedRows();
                List<OrderViewDCT> list = new List<OrderViewDCT>();
                foreach (int i in arrRowId)
                {
                    OrderViewDCT view = (OrderViewDCT)gridViewErrList.GetRow(i);
                    list.Add(view);
                }

                int intRtn = _presenter.DelErrList(list);
                if (intRtn == 0)
                {
                    this.OrderErrorList = OrderErrorList.Except(list).ToList();
                    this.DisPlayErrorList();
                }
            }

        }

        private void gridViewPhotoErrList_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                OrderViewDCT view = (OrderViewDCT)gridViewErrList.GetRow(e.RowHandle);
                if (view != null && !string.IsNullOrEmpty(view.Remark))
                {
                    e.Appearance.BackColor = Color.LightPink;
                }

            }
        }
        //progressBarReadFile.
        private int currPoint = 0;
        private void timerAnlyse_Tick(object sender, EventArgs e)
        {
            // progressBarReadFile.
            //progressBarReadFile.Minimum = 0;
            //progressBarReadFile.Maximum = 100;
            //currPoint += 10;
            //currPoint = currPoint % 100;
            //progressBarReadFile.Value = currPoint;

        }

        private void FrmImportPhoto_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (int)Keys.Escape)
            {
                _presenter.isStop = true;
                timerAnlyse.Stop();
                progressBarReadFile.Value = 0;
                this.Cursor = Cursors.Default;
                this.CurrTaskPath = new List<string>();
                _presenter.CurrTask = 0;
            }
        }

        private void gridViewPhotoList_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                GridView view = sender as GridView;

                //List<PhotoView> listView = (List<PhotoView>)view.DataSource;
                PhotoDCT photo = (PhotoDCT)view.GetRow(e.RowHandle);


                if (view != null)
                {
                    if (!string.IsNullOrEmpty(strCurrStandard))
                    {
                        if (!photo.SizeInch.Equals(strCurrStandard))
                        {
                            e.Appearance.BackColor = Color.LightPink;
                        }
                    }
                }
            }
        }
        class MyPhotoSizeComparer : IEqualityComparer<string>
        {
            #region IEqualityComparer<string> 成员

            public bool Equals(string x, string y)
            {
                string[] xWH = x.Split(new char[] { 'X' });
                string[] yWH = y.Split(new char[] { 'X' });
                if (xWH.Length <= 1 || yWH.Length <= 1)
                {
                    return true;
                }

                if ((xWH[0] == yWH[0] && xWH[1] == yWH[1]) || (xWH[0] == yWH[1] && xWH[1] == yWH[0]))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }

            public int GetHashCode(string obj)
            {
                //return obj.GetHashCode();
                return -1;
            }

            #endregion
        }

        private void gridViewPhotoList_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                if (e.Column.Name == "p_DPI")
                {
                    if ((int)e.CellValue == -1)
                    {
                        e.DisplayText = "错误";
                    }
                }
            }
            if (e.Column.Name == "gcph_dxf")
            {
                if ((bool) e.CellValue)
                {
                    e.DisplayText = "包含";
                    e.Appearance.BackColor = Color.LimeGreen;
                }
            }
        }

        [DllImport("kernel32.dll")]
        public static extern int WinExec(string exeName, int operType);

        [DllImport("shell32.dll ")]
        public static extern int ShellExecute(IntPtr hwnd, StringBuilder lpszOp, StringBuilder lpszFile, StringBuilder lpszParams, StringBuilder lpszDir, int FsShowCmd);
        PhotoDCT ForcePhoto = new PhotoDCT();
        private void gridViewPhotoList_RowCellClick(object sender, RowCellClickEventArgs e)
        {
            if (e.RowHandle > 0)
            {
                GridView view = sender as GridView;
                PhotoDCT photo = (PhotoDCT)view.GetRow(e.RowHandle);
                ForcePhoto = photo;
                clickType = 1;
                if (e.Clicks == 2)
                {

                    string infoPath = photo.PicPath;
                    //ShellExecute(IntPtr.Zero, new StringBuilder("open"), new StringBuilder("explorer.exe"),
                    //new StringBuilder(infoPath), new StringBuilder(string.Empty), 1);
                    WinExec(@"explorer /n,/select," + infoPath, 1);
                }
            }
            clickType = 0;

        }

        private void buttonRush_Click(object sender, EventArgs e)
        {
            RasterCodecs codecs = new RasterCodecs();
            //if (clickType == 0)
            //{
            int[] selectRowID = gridViewFile.GetSelectedRows();
            List<PhotoDCT> listPhoto = new List<PhotoDCT>();
            Smallerp.Contracts.DataContracts.OrderViewDCT ordedr = new OrderViewDCT();
            foreach (int rowid in selectRowID)
            {
                Smallerp.Contracts.DataContracts.OrderViewDCT odct = (OrderViewDCT)gridViewFile.GetRow(rowid);
                _presenter.RushSize(odct.PhotoS, codecs);

            }
            //}
            //else
            //{
            //    int[] selectRowID = gridViewPhotoList.GetSelectedRows();
            //    List<PhotoDCT> listPhoto = new List<PhotoDCT>();
            //    foreach (int rowid in selectRowID)
            //    {
            //        PhotoDCT photo = (PhotoDCT)gridViewPhotoList.GetRow(rowid);
            //        listPhoto.Add(photo);

            //    }
            //    _presenter.RushSize(listPhoto, codecs);

            //}
        }


        public void ShowErrorPhotoList(List<PhotoDCT> photoList)
        {
            FrmErrorPhotoCorrection frm = new FrmErrorPhotoCorrection(photoList);
            frm.ShowDialog();
        }

        private void browser1_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            IniFile.IniWriteValue("PB", "CurrSelectPath", browser1.SelectedItem.Path);

            PBDataTransfer.CurrSelectPath = browser1.SelectedItem.Path;
            //this.buttonOk.Enabled = false;
            // this.gridControlFileList.AllowDrop = false;
            this.panelOper1.Enabled = false;
            //this.panelOper.Enabled = false;
            this.gridControlFileList.Enabled = false;
            gridControlFileList.AllowDrop = false;
            this.Cursor = Cursors.WaitCursor;
            if (((List<OrderViewDCT>)gridControlFileList.DataSource).Count > 0)
            {
                _presenter.AddToDB();
            }
            gridControlFileList.Enabled = true;
            Rush();
            //this.panelOper.Enabled = true;
            this.Cursor = Cursors.Default;
            this.Close();
        }

        private void repositoryItemComboBoxphotoPaperTyye_Closed(object sender, ClosedEventArgs e)
        {
            // List<PhotoDCT> list=  (List<PhotoDCT>)gridViewPhotoList.DataSource;

            PhotoDCT photo = ForcePhoto;
            if (!string.IsNullOrEmpty(photo.PicPath))
            {
                string papername = (((DevExpress.XtraEditors.ComboBoxEdit)sender).SelectedText);
                if (string.IsNullOrEmpty(papername))
                {
                }
                else
                {
                    photo.PaperType = papername;
                    photo.PaperTypeID = TypeBind.FirstOrDefault(t => t.PaperTypeName == papername).PaperTypeId;
                }

            }
            else
            {
                MessageBox.Show("未修改成功，请先点击其他行，然后再修改该行纸型", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            }


        }

        private void repositoryItemComboBoxPaperFace_Closed(object sender, ClosedEventArgs e)
        {
            string paperFaceName = ((DevExpress.XtraEditors.ComboBoxEdit)sender).SelectedItem.ToString();
            _currOrder.PaperFace = FaceBind.FirstOrDefault(p => p.PaperFaceName == paperFaceName);
            if (!string.IsNullOrEmpty(_currOrder.OrderName))
            {
                _currOrder.PhotoS.ForEach(delegate(PhotoDCT ph)
                {
                    ph.PaperFaceId = _currOrder.PaperFace.PaperFaceId;
                    ph.PaperFace = _currOrder.PaperFace.PaperFaceName;
                });
            }
            gridControlFileList.RefreshDataSource();
        }

        private void repositoryItemComboBoxphotoPaperFace_Closed(object sender, ClosedEventArgs e)
        {
            //PhotoDCT photo = (PhotoDCT)gridViewPhotoList.GetFocusedRow();
            PhotoDCT photo = ForcePhoto;
            if (!string.IsNullOrEmpty(photo.PicPath))
            {

                string paperFace = (((DevExpress.XtraEditors.ComboBoxEdit)sender).SelectedText);
                if (string.IsNullOrEmpty(paperFace))
                {
                }
                else
                {



                    photo.PaperFace = paperFace;
                    photo.PaperFaceId = FaceBind.FirstOrDefault(t => t.PaperFaceName == paperFace).PaperFaceId;
                }
            }
            else
            {
                MessageBox.Show("未修改成功，请先点击其他行，然后再修改该行纸面", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            }
        }



        private void gridViewPhotoList_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (e.FocusedRowHandle >= 0)
            {
                GridView view = sender as GridView;
                PhotoDCT photo = (PhotoDCT)view.GetRow(e.FocusedRowHandle);
                ForcePhoto = photo;
            }
        }

        private void repositoryItemComboBoxPaperTyye_Closed(object sender, ClosedEventArgs e)
        {
            //OrderInfo oi = gvOrderProductInfo.GetFocusedRow() as OrderInfo;
            string paperTypeName = ((DevExpress.XtraEditors.ComboBoxEdit)sender).SelectedItem.ToString();
            _currOrder.PaperTypeInfo = TypeBind.FirstOrDefault(p => p.PaperTypeName == paperTypeName);
            if (!string.IsNullOrEmpty(_currOrder.OrderName))
            {
                _currOrder.PhotoS.ForEach(delegate(PhotoDCT ph)
                {
                    ph.PaperTypeID = _currOrder.PaperTypeInfo.PaperTypeId;
                    ph.PaperType = _currOrder.PaperTypeInfo.PaperTypeName;
                });
            }
            gridControlFileList.RefreshDataSource();

        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void buttonMutliIn_Click(object sender, EventArgs e)
        {
            // string tagartDir = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
            List<string> listStrDir = new List<string>();
            // Array listDir = ((System.Array) e.Data.GetData(DataFormats.FileDrop));
            List<string> paths = browser1.SelectedFilePath;

            foreach (string array in paths)
            {
                if (Directory.Exists(array))
                {
                    if (OrderViewList.FirstOrDefault(p => p.OrilOrderPath.Equals(array)) != null)
                    {
                        continue;
                    }
                    if (ImportDataTransfer.FileSavePath == "0")
                    {
                        if (File.Exists(array + "\\Properties.ini"))
                        {
                            if (MessageBox.Show("文件包 " + array + "重复导入，是否确定导入", "询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                            {
                                listStrDir.Add(array.ToString());
                            }
                        }
                        else
                        {
                            listStrDir.Add(array.ToString());
                        }
                    }
                    else
                    {
                        string orderName = array.Substring(array.LastIndexOf("\\") + 1);
                        bool isReplece = _presenter.CheckOrder(orderName);
                        if (isReplece)
                        {
                            if (MessageBox.Show("文件包 " + array + "重复导入，是否确定导入", "询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                            {
                                listStrDir.Add(array.ToString());
                            }
                        }
                        else
                        {
                            listStrDir.Add(array.ToString());
                        }

                    }

                }
            }
            this.Cursor = Cursors.WaitCursor;
            AnlyseFilePath = listStrDir;
            _presenter.CreateOrderView(listStrDir);

            this.Cursor = Cursors.Default;
        }

        private void buttonMutliOut_Click(object sender, EventArgs e)
        {
            int[] listRowID = gridViewFile.GetSelectedRows();
            if (listRowID.Length > 0)
            {
                if (DialogResult.OK == MessageBox.Show("确认删除选择的记录", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
                {
                    //OrderViewList.Remove(_currOrder);
                    //gridControlFileList.RefreshDataSource();
                    List<OrderViewDCT> ExViewList = new List<OrderViewDCT>();
                    OrderViewDCT exView;

                    foreach (int i in listRowID)
                    {
                        exView = (OrderViewDCT)gridViewFile.GetRow(i);
                        ExViewList.Add(exView);
                    }
                    OrderViewList = OrderViewList.Except(ExViewList).ToList();
                    gridControlFileList.DataSource = OrderViewList;
                    gridControlFileList.RefreshDataSource();

                }
            }
            else
            {
                MessageBox.Show("未选择任何记录", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void buttonAddRule_Click(object sender, EventArgs e)
        {
            string addStr = textBoxRule.Text.Trim();
            bool isAdd = true;
            if (!string.IsNullOrEmpty(addStr))
            {

                for (int i = 0; i < listBoxCopyRule.Items.Count; i++)
                {
                    var item = listBoxCopyRule.Items[i];
                    if (addStr.Equals((string)item))
                    {
                        isAdd = false;
                        break;
                    }
                }
                if (isAdd)
                {
                    listBoxCopyRule.Items.Add(addStr);
                    SaveRules();
                }
            }
        }
        private void SaveRules()
        {
            string strRules = "";
            for (int i = 0; i < listBoxCopyRule.Items.Count; i++)
            {
                var item = listBoxCopyRule.Items[i];
                strRules += (string)item + ",";

            }
            strRules = strRules.Trim(',');
            IniFile.FilePath = Application.StartupPath;
            IniFile.IniWriteValue("PB", "CopyRule", strRules);
            ImportDataTransfer.CopyRule = strRules;

        }

        private void buttonDelRule_Click(object sender, EventArgs e)
        {
            listBoxCopyRule.Items.Remove(listBoxCopyRule.SelectedItem);
            SaveRules();
        }

        private void checkBoxUseCopyRule_CheckedChanged(object sender, EventArgs e)
        {
            IniFile.FilePath = Application.StartupPath;
            if (checkBoxUseCopyRule.Checked)
            {
                listBoxCopyRule.Enabled = true;
                textBoxRule.Enabled = true;
                buttonAddRule.Enabled = true;
                buttonDelRule.Enabled = true;
                IniFile.IniWriteValue("PB", "UseCopyRule", "true");
                ImportDataTransfer.UseCopyRule = true;
            }
            else
            {
                listBoxCopyRule.Enabled = false;
                textBoxRule.Enabled = false;
                buttonAddRule.Enabled = false;
                buttonDelRule.Enabled = false;
                IniFile.IniWriteValue("PB", "UseCopyRule", "false");
                ImportDataTransfer.UseCopyRule = false;
            }
        }

        private void label9_Click(object sender, EventArgs e)
        {

        }

        private void buttonAUTOAdd_Click(object sender, EventArgs e)
        {

        }

        private void buttonEditFile_ButtonClick(object sender, ButtonPressedEventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
            folderBrowserDialog.SelectedPath = IniFile.DxfFolderPath;
            folderBrowserDialog.Description = "选择DXF文件夹";
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                IniFile.DxfFolderPath = folderBrowserDialog.SelectedPath;
            }
            this.buttonEditFile.Text = IniFile.DxfFolderPath;
            FileNamelist = GetDxfFile(IniFile.DxfFolderPath);
            BindDXFList();                                                                                    }
        private List<string> FileNamelist = new List<string>();
        private List<string> GetDxfFile(string inPath)
        {
            List<string> rtnList = new List<string>();
            if (System.IO.Directory.Exists(inPath))
            {
                rtnList = System.IO.Directory.GetFiles(inPath, "*.dxf",SearchOption.AllDirectories).ToList();
            }
            else
            {
                return new List<string>();
            }
            return rtnList;
        }
        private void BindDXFList()
        {
            if (FileNamelist.Count > 0)
            {
                FileNamelist = OrderDxfItem(FileNamelist, IniFile.DxfFolderPath);
            }
            listBoxControldxfList.Items.Clear();
            FileNamelist.ForEach(ea =>
            {
                listBoxControldxfList.Items.Add(ea.ToString());
            });
            ImportDataTransfer.DxfList = FileNamelist;
        }
        private List<string> OrderDxfItem(List<string> inDirList, string inStandStr)
        {
            List<string> rl = new List<string>();
            Dictionary<string, int> resultDict = new Dictionary<string, int>();
            List<string> standList = AnalyserStr(inStandStr);

            //standList.ForEach(ea => WriteLogForCheck.WriteLogFile(ea));
            foreach (string dirStr in inDirList)
            {

                List<string> dirList = AnalyserStr(dirStr);
                var r = dirList.Intersect(standList);
                resultDict.Add(dirStr, r.Count());
            }

            var rd = resultDict.OrderByDescending(or => or.Value);
            foreach (KeyValuePair<string, int> keyValuePair in rd)
            {
                rl.Add(keyValuePair.Key);
            }
            //WriteLogForCheck.WriteLogFile(rl.Count().ToString());
            return rl;
        }

        private List<string> AnalyserStr(string instr)
        {
            List<string> rtList = new List<string>();
            Analyzer analyzer = new Lucene.China.ChineseAnalyzer();
            StringReader sr = new StringReader(instr);
            TokenStream stream = analyzer.TokenStream(null, sr);

            Token t = stream.Next();
            while (t != null)
            {
                string t1 = "";

                t1 = t.ToString(); //显示格式： (字符,0,2) ，需要处理 
                t1 = t1.Replace("(", "");
                char[] separator = { ',' };
                var tep = t1.Split(separator);
                if (tep.Count() > 0)
                {
                    rtList.Add(tep[0]);
                }
                t = stream.Next();



            }
            return rtList;

        }
        private void listBoxControldxfList_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                var p = e.Location;
                //if ((p != Point.Empty) && ((Math.Abs(e.X - p.X) > SystemInformation.DragSize.Width) || (Math.Abs(e.Y - p.Y) > SystemInformation.DragSize.Height)))
                //{
                string row = listBoxControldxfList.SelectedItem as string;

                listBoxControldxfList.DoDragDrop(row, DragDropEffects.Copy);

                // 方法二
                //listBoxControl1.DoDragDrop(sender, DragDropEffects.Copy);
            }
            //}
        }

        private void listBoxControldxfList_DragOver(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }

        private void listBoxControldxfList_MouseEnter(object sender, EventArgs e)
        {

        }

        private void listBoxControldxfList_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }

        private void gridViewPhotoList_DragObjectDrop(object sender, DevExpress.XtraGrid.Views.Base.DragObjectDropEventArgs e)
        {

            //string textStr = e.Data.GetData(DataFormats.Text) as string;
        }

        private void checkBoxAutoAnlyse_CheckedChanged(object sender, EventArgs e)
        {
            IniFile.IsAutoAnlyseDXF = checkBoxAutoAnlyse.Checked;
        }

        private void buttonAnylse_Click(object sender, EventArgs e)
        {
           int[]Rows= gridViewFile.GetSelectedRows();
            foreach (int rowID in Rows)
            {
                OrderViewDCT order = gridViewFile.GetRow(rowID) as OrderViewDCT;
                foreach (PhotoDCT photoInfoPbdct in order.PhotoS)
                {
                    if (photoInfoPbdct != null && !string.IsNullOrEmpty(photoInfoPbdct.PicPath))
                    {
                        string conFile =
                            OrderDxfItem(ImportDataTransfer.DxfList, photoInfoPbdct.PicPath).FirstOrDefault();
                        photoInfoPbdct.DXFContent = DxfExtend.GetFileContent(conFile);
                        photoInfoPbdct.DXFFilePath = conFile;
                    }
                    else
                    {
                    }
                }
             
            }
            //gridControlFileList.RefreshDataSource();
            //gridViewFile.ExpandMasterRow(Rows[0]);
            //gridViewPhotoList.RefreshData();
            gridControlFileList.FocusedView.RefreshData();
        }








    }
}
